//
//  AppDelegate.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/15.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit
/*
iOS数据存储方式:
    1.偏好设置
    2.归档和解档(对象)
    3.plist(NSArray,NSDictionary)
    缺点,对数据的操作(添加,修改,删除数据不是很方便)
    4.sqlite3(数据库)
    5.CoreData(sqlite3)
*/
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //设置全局导航栏外观
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        //注意：不能有问号
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultsViewController()
        
        let userAccount = LRUserAccount.loadUserAccount()
        print("userAccount-------\(userAccount)")
        
        //window?.rootViewController = LRWelcomeController()
        //window?.rootViewController = LRNewfeatureController()
        // ?: 表示?前面的有值就执行?号后面的代码,
        // ?前面的为nil不执行后面的代码, 整个表达式返回nil
        window?.makeKeyAndVisible()
        return true
    }
    //MARK - 私有方法，切换根控制器
    private func switchRootViewController(isMain:Bool) {
        window?.rootViewController = isMain ? LRTabBarController() : LRWelcomeController()
    }
    //类方法，让外部调用切换控制器
    class func outSwitchRootViewController(isMain:Bool) {
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(isMain)
    }
    //MARK: - 判断是否是新版本
    private func isNewVersion() -> Bool {
        //1 获取当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"]
        //print("当前版本：\(currentVersion)")
        //2 获取上一次的旧版本
        let oldVersion = NSUserDefaults.standardUserDefaults().objectForKey("CFBundleShortVersionString")
        //print("老版本：\(oldVersion)")
        //不相等为YES
        let isNewVersion = !(currentVersion?.isEqual(oldVersion))!
        if isNewVersion {
            // 3. 保存当前版本
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "CFBundleShortVersionString")
            // 4 最好同步
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        // 5 返回是否新版本,不相同就是新版本
        return isNewVersion
    }
    //MARK - 判断程序启动后显示的默认控制器
    private func defaultsViewController() -> UIViewController {
        //如果用户没有登录，返回LRTabBarController,会自动跳到访客登录界面
        if !LRUserAccount.userLogin {
            return LRTabBarController()
        }
        //来到这里用户已经登录 如果是新版本就加载新特性界面，否则加载欢迎界面
        return isNewVersion() ? LRNewfeatureController() : LRWelcomeController()
    }
    

    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    func applicationDidEnterBackground(application: UIApplication) {
        //清除数据库中过时的微博数据
        LRStatusDAL.sharedInstance.clearCachedStatus()
    }
    
    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

